/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fri.businessLayer;

import fri.entiry.FrCurrency;
import fri.entiry.FrPaymentcode;
import fri.entiry.FrUsermoney;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author NguyenDongUIT
 */
@Stateless
public class UserMoneyBean extends GenericDAO<FrUsermoney>  implements UserMoneyBeanRemote {

    public UserMoneyBean() {
        super(FrUsermoney.class);
    }

    public UserMoneyBean(Class<FrUsermoney> entityClass) {
        super(entityClass);
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @Override
    public List<FrUsermoney> getAllUserMoney() {
        try {
            
            Query q = em.createQuery("SELECT f FROM FrUsermoney f where f.userId.email like '%'");
            List<FrUsermoney> result = (List<FrUsermoney>) q.getResultList();
            return result;

        } catch (Exception e) {

            System.out.println("SERVER : ==>" + e.getMessage());
        }

        return null;
    }

    @Override
    public List<FrUsermoney> getUserMoneyByUserId(String userId) {          
        
        try {
            
            Query q = em.createQuery("SELECT c FROM FrUsermoney c where c.userId.userId like '"+userId+"'");
            List<FrUsermoney> result = (List<FrUsermoney>) q.getResultList();
            return result;

        } catch (Exception e) {

            System.out.println("SERVER : ==>" + e.getMessage());
        }

        return null;
       
            
    }

    @Override
    public List<FrUsermoney> getUserMoneyByEmail(String email) {
        try {
            Query q = em.createQuery("SELECT c FROM FrUsermoney c where c.userId.email='"+email+"'");
            List<FrUsermoney> result = (List<FrUsermoney>) q.getResultList();
            return result;

        } catch (Exception e) {

            System.out.println("SERVER : ==>" + e.getMessage());
        }

        return null;
    }

    @Override
    public List<FrUsermoney> getUserIdByUserCode(String userCode) {
        try {
            Query q = em.createQuery("SELECT c FROM FrUsermoney c where c.userId.userCode='"+userCode+"'");
            List<FrUsermoney> result = (List<FrUsermoney>) q.getResultList();
            return result;

        } catch (Exception e) {

            System.out.println("SERVER : ==>" + e.getMessage());
        }

        return null;
    }

    @Override
    public int addUserMoney(FrUsermoney userMoney) {
        this.save(userMoney);
        return 1;
    }

    @Override
    public int changeUserMoney(FrUsermoney userMoney) {
        return this.update(userMoney);
    }
    
    

    @Override
    public int subtractUserMoney(String userId, float value, int currencyId) {
        
        
        FrUsermoney userMoney=getUserMoneyByUICurrency(userId, currencyId);
             
        float vl=userMoney.getVal()-value;
        Query q = em.createQuery("UPDATE  FrUsermoney c SET   c.val = '"+vl+"' where c.currencyId.currencyId = '"+currencyId+"' and c.userId.userId like '"+userId+"'");
        int result =  q.executeUpdate();
        return result;
        
        
    }

    @Override
    public FrUsermoney getUserMoneyByUICurrency(String userId, int currencyId) {
        try {
            Query q = em.createQuery("SELECT c FROM FrUsermoney  c where  c.userId.userId like '"+userId+"' and c.currencyId.currencyId = '"+currencyId+"'");
            List<FrUsermoney> result = (List<FrUsermoney>) q.getResultList();
            return result.get(0);

        } catch (Exception e) {

            System.out.println("SERVER : ==>" + e.getMessage());
        }

        return null;
    }

    @Override
    public boolean checkUserMoneyAvailable(String userId, float value, int currencyId) {
        FrUsermoney userMoney=getUserMoneyByUICurrency(userId, currencyId);
        
        if(userMoney.getVal()<value)
            return false;
        return true;
    }

    @Override
    public int deleteUserMoney(String idUserMoney) {
        try {
            Query q = em.createQuery("DELETE FROM FrUsermoney c where c.idUserMoney = '" + idUserMoney + "'");
            
            q.executeUpdate();
            
            return 1;

        } catch (Exception e) {

            System.out.println("SERVER : ==>" + e.getMessage());
        }

        return 0;
    }
    
    
    
    
    
    
    
}
